package example.telosys.util.tson;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.telosys.util.tson.Parser;

import example.telosys.util.tson.bean.BeanDate;
import example.telosys.util.tson.bean.BeanPrimitive;
import example.telosys.util.tson.bean.Data;

public class TestParser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {


		
//  "{data: "
//+ "  equipements = [ "
//+ "   {equipement: id=\"eq1\" libelle=\"Equip AAAAA\" } "
//+ "   {equipement: id=\"eq2\" libelle=\"Equip BBB\" } "
//+ "   ]"
//+ " rondes = [ "
//+ "  {ronde: id=\"r1\" libelle=\"Ronde A\" } "
//+ "  {ronde: id=\"r2\" libelle=\"Ronde B\" } "
//+ " ] "
//+ "}" 
//;
		
		;

// pointeaux = [
//  {pointeau: id="aaaa" libelle="bbbb" numFonction="" numLogic="" }
//  {pointeau: id="aaaa" libelle="bbbb" numFonction="" numLogic="" }
// ]

		
//		test1();
//		test2();
//		test3();
		test1();
	}
	
	public static HashMap getMap() 
	{
		System.out.println("Init map ...");
		HashMap map = new HashMap();
		map.put("data", Data.class );
//		map.put("alldata", AllData.class );
//		map.put("equipement", Equipement.class );
		map.put("prim", BeanPrimitive.class );
		map.put("date", BeanDate.class );
		
		return map ;
	}
	
	public static void test1() 
	{
		String s = "{data: "
			+ " beanPrimitive = "
			+ " {prim: \n iii=123 sss=12 lll=45678 bbb=88 str = \"My String\" bool = true fff=0 ddd=232  } " 
			+ " beanDate = "
//			+ " {date: utilDate='2009-11-04' sqlDate='2005-05-25' sqlTime='2006-06-16' sqlTimestamp='2007-07-17' }" 
			+ " {date: utilDate='2009-11-04 12:30:20' sqlDate='2005-05-25 05:35:55' sqlTime='2006-06-16 06:46:56' sqlTimestamp='2007-07-17 17:37:57' }" 
			+ " } ";
		//String s = "   {prim: \n iii=  \n 123 sss  = \t 12 str  =  null bool =    true } " ;

		Parser parser = new Parser( s.getBytes(), getMap() ) ;

		System.out.println("Parse ...");
		System.out.println("Input : ");
		System.out.println(s);
		Object o = parser.parse();
		
		System.out.println("Object returned : ");
		System.out.println(o);
	}
	
	public static void test2() 
	{
		String s = "[ "
			+ "   {equipement: id=\"eq1\" libelle=\"Equip AAA\\\"AA\" } "
			+ "   {equipement: id=\"eq2\" libelle=\"Equip BBBB \" } "
			+ "]";

		Parser parser = new Parser( s.getBytes(), getMap() ) ;

		System.out.println("Parse ...");
		Object o = parser.parse();
		if ( o instanceof List )
		{
			List list = (List) o ;
			Iterator it = list.iterator();
			while ( it.hasNext() )
			{
			    Object item = it.next();
				System.out.println("item : " + item.getClass().getName() + " : " + item );
			}
//			for (Object item : list) {
//				System.out.println("item : " + item.getClass().getCanonicalName() + " : " + item );
//			}
		}
		else
		{
			System.out.println("*** NOT List !");
			System.out.println(" Class : " + o.getClass().getName());
		}
	}


	public static void test4() 
	{
		String s = "{alldata: "
			+ " equipements = ["
			+ "   {equipement: id=\"eq1\" libelle=\"Equip 1AAA\" } "
			+ "   {equipement: id=\"eq2\" libelle=\"Equip 2BBB\" } "
			+ "   {equipement: id=\"eq3\" libelle=\"Equip 3CCC\" } "
			+ "   ]"
			+ " rondes = ["
			+ "   {ronde: id=\"r1\" libelle=\"Ronde 111 \" }  "
			+ "   {ronde: id=\"r2\" libelle=\"Ronde 222 \" }  "
			+ "   ]"
			+ " } ";

		Parser parser = new Parser( s.getBytes(), getMap() ) ;

		System.out.println("Parse ...");
		Object o = parser.parse();

//		if ( o instanceof AllData )
//		{
//			System.out.println("AllData : ");
//			AllData data = (AllData) o ;
//			LinkedList<Ronde> r = data.getRondes();
//			LinkedList<Equipement> e = data.getEquipements();
//			System.out.println("Equipements : size = " + e.size() );
//			System.out.println(e);
//			System.out.println("Rondes      : size = " + r.size() );
//			System.out.println(r);
//		}
//		else
//		{
//			System.out.println("*** NOT AllData !");
//			System.out.println(" Class : " + o.getClass().getCanonicalName());
//		}
	}
}
